﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels;
using System.Configuration;
using Emerson.Storage.Agent.Core;
using System.Reflection;

namespace Emerson.Storage.Agent
{
    public partial class AgentForm : Form
    {
        AgentService service;

        public AgentForm()
        {
            InitializeComponent();
            service = new AgentService();

            InitializeDevice();
        }

        private void InitializeDevice()
        {
            service.ScanDevice();
            List<BatteryInfo> batteries = service.GetBatteryInfos();
            listBoxAvailableDevices.DataSource = batteries;
            listBoxAvailableDevices.DisplayMember = "BatteryType";
        }

        private void buttonRefresh_Click(object sender, EventArgs e)
        {
            List<BatteryInfo> bats = service.GetBatteryInfos();

            int selectedIndex = listBoxAvailableDevices.SelectedIndex;
            listBoxAvailableDevices.DataSource = bats;
            listBoxAvailableDevices.DisplayMember = "BatteryType";

            if (selectedIndex < listBoxAvailableDevices.Items.Count)
                listBoxAvailableDevices.SelectedIndex = selectedIndex;
        }

        private void listBoxAvailableDevices_SelectedIndexChanged(object sender, EventArgs e)
        {
            BatteryInfo bi = (BatteryInfo)listBoxAvailableDevices.SelectedItem;
            string batteryInfo = bi.ToString();
            textBoxDetail.Text = batteryInfo;
        }
    }
}
